package com.example.demo31;


import static com.example.demo31.R.id.btn_1;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.List;

public class checkboxActivity extends AppCompatActivity {

    private CheckBox checkBox1,checkBox2,checkBox3;

    private Button btn;

    private TextView res;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_checkbox);

        checkBox1 = findViewById(R.id.checkbox_btn1);
        checkBox2 = findViewById(R.id.checkbox_btn2);
        checkBox3 = findViewById(R.id.checkbox_btn3);

        btn = findViewById(R.id.btn_1);

        res = findViewById(R.id.Text_btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> selectionhobby = new ArrayList<>();
                if(checkBox1.isChecked())
                {
                    selectionhobby.add(checkBox1.getText().toString());
                }
                if(checkBox2.isChecked())
                {
                    selectionhobby.add(checkBox2.getText().toString());
                }
                if(checkBox3.isChecked())
                {
                    selectionhobby.add(checkBox3.getText().toString());
                }
                if(selectionhobby.isEmpty())
                {
                    Toast.makeText(checkboxActivity.this,"请至少选择一个爱好", Toast.LENGTH_SHORT).show();
                    return;
                }
                res.setText("你的爱好是"+selectionhobby.toString());


            }
        });



        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
}